package com.ruoyi.business.controller;

import com.ruoyi.business.VO.*;
import com.ruoyi.business.service.IBuCustomService;
import com.ruoyi.business.service.IBuEarningsRecordsService;
import com.ruoyi.business.service.IBuEarningsService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/home")
public class HomeController extends BaseController {
    @Autowired
    private IBuEarningsService buEarningsService;
    @Autowired
    private IBuCustomService buCustomService;
    @Autowired
    private IBuEarningsRecordsService buEarningsRecordsService;
    @Autowired
    private ISysUserService userService;

    //总收益
    @GetMapping ("/sum")
    @Anonymous
    public AjaxResult sum(){
        StatisticsVO statisticsVO = buEarningsService.selectSum();
        if (statisticsVO.getEarningsAmount()!= null){
            statisticsVO.setEarningsAmount(statisticsVO.getEarningsAmount().divide( BigDecimal.valueOf( 10000 ), 2, RoundingMode.HALF_UP));
        }else {
            statisticsVO.setEarningsAmount(BigDecimal.valueOf(0));
        }
        if (statisticsVO.getConserveElectricity()!=null&&statisticsVO.getConserveElectricity()!=0) {
            statisticsVO.setConserveElectricity( statisticsVO.getConserveElectricity() / 10000 );
        }else {
            statisticsVO.setConserveElectricity(0);
        }
        if (statisticsVO.getCarbonEmissions()!=null&&statisticsVO.getCarbonEmissions()!=0) {
            statisticsVO.setCarbonEmissions( statisticsVO.getCarbonEmissions() / 10000 );
        }else {
            statisticsVO.setCarbonEmissions(0);
        }
        return success(statisticsVO);
    }

    //城市代理实时分润
    @GetMapping("/city")
    @Anonymous
    public AjaxResult city(){
        //List<CityVO> cityVOList = buEarningsRecordsService.city();
        List<CityVO> cityVOList = new ArrayList<>();
        CityVO cityVO = new CityVO();
        cityVO.setName("部门");
        cityVO.setValue( BigDecimal.valueOf( 100 ) );
        cityVOList.add( cityVO );
        CityVO cityVO2 = new CityVO();
        cityVO2.setName("部门");
        cityVO2.setValue( BigDecimal.valueOf( 100 ) );
        cityVOList.add( cityVO2 );
        return success(cityVOList);
    }

    //统计客户类型数量city
    @GetMapping("/custom")
    @Anonymous
    public AjaxResult custom(){
        List<CustomVO> customVO = buCustomService.custom();
        List<String> customType = new ArrayList<>();
        List<Integer> customNum = new ArrayList<>();
        for (CustomVO customVO1 : customVO){
            customType.add( customVO1.getCustomType() );
            customNum.add( customVO1.getCustomNum() );
        }
        Map<String, Object> map = new HashMap<>();
        map.put("customType", customType);
        map.put("customNum", customNum);
        return success(customVO);
    }

    //各地区总收益
    @GetMapping("/area")
    @Anonymous
    public AjaxResult area(){
        List<AreaVO> areaVOList = buCustomService.area();
        List<String> areaName = new ArrayList<>();
        List<String> earningsAmount = new ArrayList<>();
        for (AreaVO areaVO : areaVOList){
            if (areaVO.getEarningsAmount()!= null){
                areaVO.setEarningsAmount(areaVO.getEarningsAmount().divide( BigDecimal.valueOf( 10000 ), 2, RoundingMode.HALF_UP));
            }else {
                areaVO.setEarningsAmount(BigDecimal.valueOf(0));
            }
            areaName.add( areaVO.getAreaName() );
            earningsAmount.add( String.valueOf( areaVO.getEarningsAmount() ) );
        }
        Map<String, Object> map = new HashMap<>();
        map.put("name", areaName);
        map.put("value", earningsAmount);
        return success(map);
    }

    //销售精英排行榜Top10
    @GetMapping("/top")
    @Anonymous
    public AjaxResult top(){
        List<TopVO> topVOList = buEarningsRecordsService.top();
        int i = 1;
        for (TopVO topVO : topVOList){
            topVO.setTop( i++ );
        }
        return success(topVOList);
    }

    //近半年每月的收益
    @GetMapping("/month")
    @Anonymous
    public AjaxResult month(){
        List<MonthVO> monthVOList = buEarningsService.month();
        return success(monthVOList);
    }

    //收益排行榜
    @GetMapping("/rank/{type}")
    @Anonymous
    public AjaxResult rank(@PathVariable String type){
        List<RankVO> rankVOList = buEarningsService.rank(type);
        return success(rankVOList);
    }
}
